package com.example.iitematicviewer.api.machine;

import com.alibaba.fastjson.JSONObject;
import com.example.iitematicviewer.constant.Constant;
import com.example.iitematicviewer.util.SnowFlakeGenerateIdWorker;
import com.example.iitematicviewer.vo.MachineVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 归档转换类
 */
@Service
public class MachineTransService {

    public MachineVO transJSONObjectByUpdate(JSONObject param){
        MachineVO machineVO = new MachineVO();

        //主键
        String id = param.getString(Constant.id);
        machineVO.setId(id);

        //稿件名称
        String machineName = param.getString(MachineConstant.title);
        if (StringUtils.isNotEmpty(machineName)) {
            machineVO.setMachineName(machineName);
        }

        //稿件文件链接
        String videoLink = param.getString(MachineConstant.videoLink);
        if (StringUtils.isNotEmpty(videoLink)) {
            machineVO.setMachineLink(videoLink);
        }

        //投稿编号
        String archiveNumber = param.getString(MachineConstant.archiveNumber);
        if (StringUtils.isNotEmpty(archiveNumber)) {
            machineVO.setMachineNo(archiveNumber);
        }

        //投稿时间
        machineVO.setMachineSendTime(getNowDate());

        //投稿人
        String author = param.getString(MachineConstant.author);
        if (StringUtils.isNotEmpty(author)) {
            machineVO.setMachineAuthor(author);
        }

        //审核状态
        machineVO.setOperateType("0");

        //稿件类型
        String submissionType = param.getString(MachineConstant.submissionType);
        if (StringUtils.isNotEmpty(submissionType)) {
            machineVO.setType(submissionType);
        }

        //操作人
        String archived = param.getString(MachineConstant.archived);
        if (StringUtils.isNotEmpty(archived)) {
            machineVO.setOperator(archived);
        }

        //删除状态
        machineVO.setDr("0");
        return machineVO;
    }

    /**
     * 将JSONObject转换成实体
     * @param param
     * @return
     */
    public MachineVO transJSONObjectByMachine(JSONObject param){
        MachineVO machineVO = new MachineVO();

        //主键
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker =
                new SnowFlakeGenerateIdWorker(0L, 0L);
        String id = snowFlakeGenerateIdWorker.generateNextId();
        machineVO.setId(id);

        //稿件名称
        String machineName = param.getString(MachineConstant.title);
        if (StringUtils.isNotEmpty(machineName)) {
            machineVO.setMachineName(machineName);
        }

        //稿件文件链接
        String videoLink = param.getString(MachineConstant.videoLink);
        if (StringUtils.isNotEmpty(videoLink)) {
            machineVO.setMachineLink(videoLink);
        }

        //投稿编号
        String archiveNumber = param.getString(MachineConstant.archiveNumber);
        if (StringUtils.isNotEmpty(archiveNumber)) {
            machineVO.setMachineNo(archiveNumber);
        }

        //投稿时间
        machineVO.setMachineSendTime(getNowDate());

        //投稿人
        String author = param.getString(MachineConstant.author);
        if (StringUtils.isNotEmpty(author)) {
            machineVO.setMachineAuthor(author);
        }

        //审核状态
        machineVO.setOperateType("0");

        //稿件类型
        String submissionType = param.getString(MachineConstant.submissionType);
        if (StringUtils.isNotEmpty(submissionType)) {
            machineVO.setType(submissionType);
        }

        //操作人
        String archived = param.getString(MachineConstant.archived);
        if (StringUtils.isNotEmpty(archived)) {
            machineVO.setOperator(archived);
        }

        //删除状态
        machineVO.setDr("0");
        return machineVO;
    }


    /**
     * 获取当前时间
     * @return
     */
    private String getNowDate(){
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss");
        return dateFormat.format(date);
    }
}
